package nl.saxion.security.config;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Properties;

import nl.saxion.security.encryption.TransformerAES;
import nl.saxion.security.init.Main;

public final class INIConfig implements Config {
	
	private Properties p = null;
	
	private Properties getProperties()
	{
		if(p == null)
		{
			File f = new File("config.ini");
			
			if(f.isFile() && f.canRead())
			{
				try
				{
					Properties tmp = new Properties();
					tmp.load(new FileInputStream(f));
					
					p = tmp;
				} catch(Exception e)
				{}
			}
			
			if(p == null)
			{
				CreateConfig();
			}
		}
		
		return p;
	}
	
	private void setProperties(String aKey, String aValue) {
		if(aKey != null && aValue != null) {
			File f = new File("config.ini");
			
			if(f.isFile() && f.canRead()) {
				try {
					Properties tmp = getProperties();					
					tmp.setProperty(aKey, aValue);
					FileWriter txt = new FileWriter(f);
					tmp.store(txt, "");
					
				}
				catch(Exception e) {
					System.out.print(e);
				}
			}
		}
	}
	
	/**
	 * Get by key
	 * @param key The key
	 * @return The value (default when not found)
	 */
	public Object get(String key, Object defaultObject)
	{
		Object result = getProperties().get(key);
		if(result == null)
		{
			result = defaultObject;
		}
		return result;
	}
	
	/**
	 * Get by key as String
	 * @param key The key
	 * @return The value (default when not found)
	 */
	public String getAsString(String key, String defaultString)
	{
		String result = defaultString;
		try
		{
			result = String.valueOf(get(key, defaultString));
		} catch(Exception e)
		{}
		return result;
	}
	
	/**
	 * Get by key as integer
	 * @param key The key
	 * @return The integer value (default when not found or invalid)
	 */
	public int getAsInt(String key, int defaultInt)
	{
		int result = defaultInt;
		try
		{
			String defaultString = "" + defaultInt;
			result = Integer.valueOf(getAsString(key, defaultString));
		} catch(Exception e)
		{}
		return result;
	}
	
	/**
	 * Gets the key as boolean
	 * @param key The key
	 * @return The boolean value (default when not found)
	 */
	public boolean getAsBoolean(String key, boolean defaultBoolean)
	{
		String defaultString = defaultBoolean ? "true" : "false";
		return getAsString(key, defaultString).equals("true");
	}
	
	/**
	 * 
	 * @param Key
	 * @param defaultString
	 */
	public void setAsString(String Key, String defaultString) {
		setProperties(Key, defaultString);
	}
	
	public void CreateConfig() {
		if(p == null)
		{
			File f = new File("config.ini");
			
			if(!f.isFile() && !f.canRead())
			{
				Properties tmp = new Properties();
				tmp.setProperty(Config.KEY_GUI_FONT_FAMILY, "\"Helvetica\"");
				tmp.setProperty(Config.KEY_GUI_TITLE_FONT_FAMILY, "\"Helvetica\"");
				tmp.setProperty(Config.KEY_GUI_FONT_SIZE, "12");
				tmp.setProperty(Config.KEY_GUI_TITLE_FONT_SIZE, "24");
				tmp.setProperty(Config.KEY_GUI_HEIGHT, "500");
				tmp.setProperty(Config.KEY_GUI_WIDTH, "500");
				TransformerAES tfAES = new TransformerAES();
				byte[] key = null;
				try {
					key = tfAES.generateKey("Key");
				} catch (Exception e) {					
					Main.close("Could not load config file");
				}
				
				String testKey = tfAES.asHex(key);
				tmp.setProperty(Config.KEY_SYNC_ENCR_PASS, testKey);
				tmp.setProperty(Config.KEY_SYNC_ENCR_KEY, testKey);
				tmp.setProperty(Config.KEY_SYNC_PATH, "C:/");
				
				try {
					f.createNewFile();
					FileWriter txt = new FileWriter(f);
					tmp.store(txt, "");
					txt.close();
					tmp.clear();
				} catch (IOException e) {					
					Main.close("Could not load config file");
				}
			}
		}
		
	}
	
}
